package com.doit.demo.day01;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

/**
 * @DATE 2022/2/14/0:11
 * @Author MDK
 * @Version 2021.2.2
 *
 *
 *  理解并行度的概念
 *      1.flink使用资源槽对内存进行隔离,一个TaskManager可以有多个TaskSlot
 *      2.提交的job并行度要小于等于集群中可用的槽的数量
 *      3.如果只在集群中提交任务,必须提前指定并行度,比如使用命令提交需要由-p参数指定
 *      4.如果只在本地运行,就不需要指定(因为有默认并行度)
 **/
public class ParallelismDemo {
    public static void main(String[] args) throws Exception {

        //将端口号设置为固定端口号
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);

        //活动当前执行环境默认的并行度(当前机器的CPU逻辑核数,即CPU线程数)
        int parallelism = env.getParallelism();
        System.out.println("当前环境并行度为:"+parallelism);

        //获取算子所在的task的并行度(DataStream的并行度永远为1)
        DataStreamSource<String> lines = env.socketTextStream("linux01", 8888);
        int parallelism1 = lines.getParallelism();
        System.out.println("SocketSource的并行度为:"+parallelism1);

        //map算子所在Task的并行度(需要人为指定该算子的并行度,未指定默认与执行环境并行度保持一致)
        SingleOutputStreamOperator<String> upperStream = lines.map(new MapFunction<String, String>() {
            @Override
            public String map(String line) throws Exception {
                return line.toUpperCase();
            }
        }).setParallelism(2);

        //获取map算子对应的并行度
        int parallelism2 = upperStream.getParallelism();
        System.out.println("map算子对应的并行度:" + parallelism2);

        //sink算子所在task的并行度(需要人为指定该算子的并行度,未指定默认与执行环境并行度保持一致)
        DataStreamSink<String> streamSink = upperStream.addSink(new RichSinkFunction<String>() {
            @Override
            public void invoke(String value, Context context) throws Exception {
                //sink算子每来一条数据,调用一次invoke方法
                //获取当前subtask的index,然后将index+1
                //getRuntimeContext方法可以获取当前正在运行的subtask的很多信息
                int indexOfThisSubtask = getRuntimeContext().getIndexOfThisSubtask();
                System.out.println(indexOfThisSubtask + ">" + value);
            }
        });

        int parallelism3 = streamSink.getTransformation().getParallelism();
        System.out.println("sink算子所在的subtask并行度:"+parallelism3);

        env.execute();

    }
}
